<template>
  <div class="articaldetail">
    <div class="header">
      <div class="left">
        <van-icon name="arrow-left back" @click="$router.back()" />
        <span class="iconfont iconnew new"></span>
      </div>
      <span v-if="article.has_follow === false" @click="toggleFollow"
        >关注</span
      >
      <span class="has-followed" v-else @click="toggleFollow">已关注</span>
    </div>
    <div class="detail">
      <!-- 文章标题 -->
      <div class="title">{{ article.title }}</div>
      <div class="desc">
        <!-- 用户昵称 -->
        <span>{{ article.user && article.user.nickname }}</span> &nbsp;&nbsp;
        <!-- 创建时间 -->
        <span>{{ article.create_date }}</span>
      </div>
      <!-- 文章内容 -->
      <div class="content" v-html="article.content"></div>
      <div class="opt">
        <span class="like">
          <!-- 点赞效果 -->
          <van-icon
            name="good-job-o"
            @click="likesBtn"
            :class="{ giveLikes: goodsLikes }"
            v-model="get"
          />点赞{{ num }}
        </span>
        <span class="chat"> <van-icon name="chat" class="w" />微信 </span>
      </div>
    </div>
    <!-- 精彩跟帖 -->
    <div class="keeps">
      <h2>精彩跟帖</h2>
      <div class="item">
        <div class="head">
          <img src="../assets/logo.png" alt />
          <div>
            <p>火星网友</p>
            <span>2小时前</span>
          </div>
          <span>回复</span>
        </div>
        <div class="text">文章说得很有道理</div>
      </div>
      <div class="more" @click="$router.push(`/commentList/${article.id}`)">
        更多跟帖
      </div>
    </div>
    <!-- 填充被底栏盖住的空间 -->
    <div class="bottom-space"></div>
    <!-- 底部评论栏 -->
    <!-- comment_length字段表示评论数量 -->
    <bottom-comment-bar
      :comment-length="article.comment_length"
    ></bottom-comment-bar>
  </div>
</template>

<script>
// 底部评论栏组件
import bottomCommentBar from "@/components/bottom-comment-bar.vue";
import { getArticleDetail } from "../api/news";
import { followUser, cancelFollowUser,likesArticleDetail } from "../api/user";
export default {
  components: { "bottom-comment-bar": bottomCommentBar },
  data() {
    return {
      article: {},
      goodsLikes: false,
      num: 0,
      get: false,
    };
  },
  created() {
    this.loadArticleDetail();
  },
  methods: {
    loadArticleDetail() {
      getArticleDetail(this.$route.params.id).then((res) => {
        // console.log(res);
        this.article = res.data.data;
      });
    },
    toggleFollow() {
      if (this.article.has_follow) {
        cancelFollowUser(this.article.user.id).then(() => {
          this.$toast.success("保存成功");
          this.loadArticleDetail();
        });
      } else {
        followUser(this.article.user.id).then(() => {
          this.$toast.success("保存成功");
          this.loadArticleDetail();
        });
      }
    },
    // 点赞效果
    likesBtn() {
      this.get = !this.get
      if (this.get ==true) {
        this.$toast("点赞成功");
        this.num++;
        this.goodsLikes=true
      } else {
          this.$toast("取消点赞");
          this.goodsLikes = false;
          this.num--;
          // 只需要调用一次
          likesArticleDetail(this.$route.params.id).then((res) => {
      });}
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  > .left {
    height: 100%;
    vertical-align: middle;
    position: relative;
    .van-icon {
      position: absolute;
      top: 15px;
      left: 0;
    }
    > span {
      margin-left: 22px;
      font-size: 50px;
    }
  }
  > span {
    padding: 5px 15px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
  }
}
.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
  .content {
    text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
  }
}
.opt {
  display: flex;
  justify-content: space-around;
  .like,
  .chat {
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  .w {
    color: rgb(84, 163, 5);
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: (50/360) * 100vw;
        height: (50/360) * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
  .more {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px auto;
    font-size: 13px;
  }
}
/deep/ .photo img {
  display: block;
  width: 100%;
}

.header > span.has-followed {
  border: 1px solid #000;
  background-color: #fff;
  color: #000;
}

.bottom-space {
  height: 50px;
}
.giveLikes {
  color: red;
}
</style>